<?php
declare(strict_types=1);

namespace app\salesman\controller;


use app\model\AgentSalesman;
use app\salesman\model\Salesman;
use app\salesman\validate\Salesman as SalesmanValidate;

class User extends Controller
{
    /**
     * 用户信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info() {
        $salesmanInfo = $this->salesman->toArray(); // 基础数据
        $model = new Salesman();
        $agentDetail = $model->agentDetail($salesmanInfo['id']); // 代理商详情

        // 菜单
        $permissions = [
            [
                'permissionId' => 'user',
                'permissionName' => '首页',
            ],
            [
                'permissionId' => 'order',
                'permissionName' => '订单管理',
            ],
            [
                'permissionId' => 'mobile',
                'permissionName' => '话费订单',
            ],
            [
                'permissionId' => 'stat',
                'permissionName' => '业绩统计',
            ],
        ];

        $info = [
            'agent_detail' => $agentDetail,
            'agent_num' => count($agentDetail),
            'role' => ['permissions' => $permissions],
        ];

        $info = array_merge($salesmanInfo, $info);

        return success('操作成功', $info);
    }

    /**
     * 修改密码
     */
    public function updatePassword()
    {
        $data = input('post.');
        $validate = new SalesmanValidate;
        if (!$validate->scene('updatePassword')->check($data)) {
            return fail($validate->getError());
        }
        $model = new Salesman();
        $data['salesman_id'] = $this->salesman['id'] ?? '';
        return $model->updatePassword($data);
    }
}